package org.liuhy
{
	import flash.events.MouseEvent;
	import flash.geom.Point;
	
	import mx.collections.ArrayCollection;
	import mx.containers.Canvas;
	import mx.controls.Image;
	import mx.core.UIComponent;
	import mx.events.DragEvent;
	import mx.managers.DragManager;
	
	import org.CacheUtils;
	import org.liuhy.line.AbstractLine;
	import org.liuhy.line.CLine;
	import org.liuhy.line.LineNode;
	import org.liuhy.line.rect.RectLine;
	import org.liuhy.node.FlowNode;
	import org.liuhy.node.focus.FocusNode;
	import org.liuhy.utils.FlowNodeUtils;
	//流程设计器，设计面板
	public class FlowPanel extends Canvas
	{
		public function FlowPanel()
		{
			this.addEventListener(DragEvent.DRAG_ENTER,onDragEnter);
			this.addEventListener(DragEvent.DRAG_DROP,onDragDrop);
			this.addEventListener(DragEvent.DRAG_OVER,onDragOver);
			this.addEventListener(MouseEvent.CLICK,onCanvasClick);
		}
		public function validate():Boolean{
			var b_isOk:Boolean = true;
			var _array:Array = this.getChildren();
			for(var i:int=0;i<_array.length;i++){
				var _obj:Object = _array[i];
				if(_obj is FlowNode){
					//判断是否有孤单流程节点;
					var _flowNode:FlowNode = _obj as FlowNode;
					if(""==_flowNode.title){
						//流程节点名称不能为空;(也不能相同)
					}
					if(_flowNode._inLineList.length<1 && _flowNode._outLineList.length<1){
						//有问题阿;
						b_isOk = false;
						_flowNode.borderColor = 0xFF0000;
						_flowNode.focusNodeBorderColor = 0xFF0000;
						_flowNode.focusNodeBackgroundColor = 0xFF0000;
						_flowNode.draw();
					}
				}else if(_obj is CLine){
					var _cLine:CLine = _obj as CLine;
					//孤线;
					if(null==_cLine.getArrowLine().in_flowFocusNode || null==_cLine.getFromLine().out_flowFocusNode){
						//存在问题;
						b_isOk = false;
						_cLine.lineColor = 0xFF0000;
						_cLine.lineNodeBorderColor = 0xFF0000;
						_cLine.lineNodeBackgroundColor = 0xFF0000;
						_cLine.draw();
					}
				}
			}
			return b_isOk;
		}
		//显示或隐藏编辑焦点;
		public function setFocusVisible(p_bool:Boolean):void{
			var _array:Array = this.getChildren();
			for(var i:int=0;i<_array.length;i++){
				var _ui:Object = _array[i] as Object;
				if(_ui is FlowNode){
					var _flowNode:FlowNode = _ui as FlowNode;
					_flowNode.setFocusVisible(p_bool);
				}else if(_ui is CLine){
					(_ui as CLine).setFocusVisible(p_bool);
				}
			}
		}
		public function onDragEnter(event:DragEvent):void{
			if(this.enabled==true){
				DragManager.acceptDragDrop(UIComponent(event.currentTarget));
			}
		}
		public function onDragDrop(event:DragEvent):void{
			if(event.dragInitiator is FocusNode){
				var _focusNode:FocusNode = event.dragInitiator as FocusNode;
				_focusNode.adjustFlowNode(event);
			}else if(event.dragInitiator is LineNode){
				var _lineNode:LineNode = event.dragInitiator as LineNode;
				_lineNode.lineNodeDrop(event);
			}else if(event.dragInitiator is Image){
				//
				CacheUtils.push(this.exportXML());
				var _img:Image = event.dragInitiator as Image;
				var _graphic:UIComponent = GraphicFactory.createNEW(_img.id,new Point(event.localX,event.localY));
				this.addChild(_graphic);
				
			}else if(event.dragInitiator is FlowNode){
				var _flowNode:FlowNode = event.dragInitiator as FlowNode;
				_flowNode.flowNodeDrop(event);
			}
		}
		public function onDragOver(event:DragEvent):void{
			if(event.dragInitiator is LineNode){
				var _lineNode:LineNode = event.dragInitiator as LineNode;
				_lineNode.lineNodeVirtualDrag(event);
			}
		}
		public function importXML(p_xml:String):void{
				var _xml:XML = new XML(p_xml);
				var _flowNodeList:ArrayCollection = new ArrayCollection();
				//创建节点;
				for(var i:int=0;i<_xml.nodes.node.length();i++){
//					var _flowNode:FlowNode = new RectFlowNode(int(_xml.nodes.node[i].@width),int(_xml.nodes.node[i].@height));
					var _flowNode:FlowNode = GraphicFactory.getFlowNode(_xml.nodes.node[i].@type.toString(),int(_xml.nodes.node[i].@x),int(_xml.nodes.node[i].@y),int(_xml.nodes.node[i].@width),int(_xml.nodes.node[i].@height));
					_flowNode.flowNodeId   = _xml.nodes.node[i].@name.toString();//标题；
					_flowNode.title		   = _xml.nodes.node[i].@name.toString();//标题；
//					_flowNode.x			   =  _xml.nodes.node[i].@x.toString();
//					_flowNode.y			   =  _xml.nodes.node[i].@y.toString();
					_flowNode.draw();
					this.addChild(_flowNode);
					_flowNodeList.addItem(_flowNode);				
				}
				//创建连接线;
				for(var i:int=0;i<_xml.actions.action.length();i++){					
					var _cLine:CLine = new CLine();
					_cLine.x		= _xml.actions.action[i].@x;
					_cLine.y 		= _xml.actions.action[i].@y;
					_cLine.title 	= _xml.actions.action[i].@title;
					for(var j:int=0;j<_xml.actions.action[i].line.length();j++){
						var x1:int = _xml.actions.action[i].line[j].@x1;
						var y1:int = _xml.actions.action[i].line[j].@y1;
						var x2:int = _xml.actions.action[i].line[j].@x2;
						var y2:int = _xml.actions.action[i].line[j].@y2;
						var _uLine:RectLine = new RectLine(x1,y1,x2,y2,_cLine);
					}
					_cLine.initDraw();
					_cLine.adjustTitle(null);
					var _from:String 	 	= _xml.actions.action[i].@from;
					var _from_pos:String	= _xml.actions.action[i].@from_pos;
					var _arrow:String 		= _xml.actions.action[i].@arrow;	
					var _arrow_pos:String 	= _xml.actions.action[i].@arrow_pos;	
			
					if(null!=_from&&""!=_from){
						var _flowNode:FlowNode = FlowNodeUtils.getFlowNodeById(_from,_flowNodeList);
						var _fromLine:AbstractLine = _cLine.getFromLine();
						if(null!=_flowNode){
							_fromLine.out_flowFocusNode = _flowNode.getFocusNode(_from_pos);
							_flowNode.addOutLine(_fromLine);
						}
					}
					if(null!=_arrow&&""!=_arrow){
						var _flowNode:FlowNode = FlowNodeUtils.getFlowNodeById(_arrow,_flowNodeList);
						var _arrowLine:AbstractLine = _cLine.getArrowLine();
						if(null!=_flowNode){
							_arrowLine.in_flowFocusNode = _flowNode.getFocusNode(_arrow_pos);
							_flowNode.addInLine(_arrowLine);
						}
					}
					this.addChild(_cLine);
				}
			}
			//文本框中显示图形界面后的xml内容
			public function exportXML():String{
				var _cArray:Array = this.getChildren();
				var _cLineArray:ArrayCollection = new ArrayCollection();
				var _cFlowNodeArray:ArrayCollection = new ArrayCollection();
				var _xml:String = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<flow>\n";
				var _xml_nodes:String = "";
				var _xml_actions:String = "";
				for(var i:int=0;i<_cArray.length;i++){
					var _obj:Object = _cArray[i];
					if(_obj is FlowNode){
						var _fn:FlowNode = _obj as FlowNode;
						_xml_nodes = _xml_nodes + "<node type='"+_fn.type+"' "+ " id='"+ _fn.title + "' name='" + _fn.title + "' x='" + _fn.x +"' y='" + _fn.y +"' width='" + _fn.width +"' height='" +_fn.height +"'>";
						_cFlowNodeArray.addItem(_obj);
						_xml_nodes = _xml_nodes + "</node>\n";
					}else if(_obj is CLine){
						var _cLine:CLine = _obj as CLine;
						var _from:String 		= "";
						var _from_pos:String 	= "";
						if(null!=_cLine.getFromFlowNode()){
							_from		 = _cLine.getFromFlowNode().title;
							_from_pos 	 = _cLine.getFromLine().out_flowFocusNode.pos;
						}
						var _arrow:String 		= "";
						var _arrow_pos:String   = "";
						if(null!=_cLine.getArrowFlowNode()){
							_arrow 		=_cLine.getArrowFlowNode().title;
							_arrow_pos 	= _cLine.getArrowLine().in_flowFocusNode.pos;
						}						
						_xml_actions = _xml_actions + "<action id='' name='' title='" + _cLine.title +"' from='" + _from +"'" + " from_pos='" + _from_pos + "' arrow='" + _arrow +"' arrow_pos='" +_arrow_pos+"' x='"+_cLine.x + "' y='"+_cLine.y +"'>\n";
						var _line:AbstractLine = _cLine.getFromLine();
						while(null!=_line){
							_xml_actions = _xml_actions + "<line x1='" + _line.x1 + "' y1='"+_line.y1 + "' x2='" +_line.x2+"' y2='"+_line.y2+"'></line>\n";
							_line=_line.nextLine;
						}
						_xml_actions = _xml_actions + "</action>\n";
					}
				}
				_xml = _xml + "<nodes>\n" + _xml_nodes + "</nodes>\n" + "<actions>\n"+ _xml_actions + "</actions>\n" + "</flow>";
				return _xml; 
			}
		public function onCanvasClick(event:MouseEvent):void{
			var _array:Array = this.getChildren();
			for(var i:int=0;i<_array.length;i++){
				if(_array[i] is FlowNode){
					(_array[i] as FlowNode).setFocusVisible(false);
				}else if(_array[i] is CLine){
					(_array[i] as CLine).setFocusVisible(false);
				}
			}
			this.stage.focus = this;			
		}			
	}
}